﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;

namespace ContosoWebParts.ListInspector
{
    public partial class ListInspectorUserControl : UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        }

        protected Guid SelectedListID = Guid.Empty;
        protected bool UpdateListProperties = false;

        protected void lslLists_SelectedIndexChanged(object sender, EventArgs e)
        {
            SelectedListID = new Guid(lstLists.SelectedValue);
            UpdateListProperties = true;

        }

        protected void Page_PreRender(object sender, EventArgs e)
        {
            if ((lstLists.SelectedIndex > -1) & (!UpdateListProperties))
            {
                SelectedListID = new Guid(lstLists.SelectedValue);
            }

            lstLists.Items.Clear();
            SPWeb site = SPContext.Current.Web;
            foreach (SPList list in site.Lists)
            {
                ListItem listItem = new ListItem(list.Title, list.ID.ToString());
                lstLists.Items.Add(listItem);
            }
            if (SelectedListID != Guid.Empty)
            {
                lstLists.Items.FindByValue(SelectedListID.ToString()).Selected = true;
            }
            if (UpdateListProperties)
            {
                SPList list = SPContext.Current.Web.Lists[SelectedListID];
                lblListTitle.Text = list.Title;
                lblListID.Text = list.ID.ToString().ToUpper();
                lblListIsDocumentLibrary.Text =
                    (list is SPDocumentLibrary).ToString();
                lblListIsHidden.Text = list.Hidden.ToString();
                lblListItemCount.Text = list.ItemCount.ToString();
                lnkListUrl.Text = list.DefaultViewUrl;
                lnkListUrl.NavigateUrl = list.DefaultViewUrl;
            }

        }

    }
}
